package com.rupertjones.globalcron.web.util;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatterBuilder;

import static org.joda.time.format.DateTimeFormat.forPattern;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
public class JodaTimeFormatter {
    public static String format(Period period) {
        org.joda.time.format.PeriodFormatter output = new PeriodFormatterBuilder()
                .appendDays()
                .appendSuffix(" day", " days")
                .appendSeparator(", ")
                .appendHours()
                .appendSuffix(" hour", " hours")
                .appendSeparator(", ")
                .appendMinutes()
                .appendSuffix(" minute", " minutes")
                .appendSeparator(", ")
                .appendSeconds()
                .appendSuffix(" second", " seconds")
                .toFormatter();
        return output.print(period);
    }

    /**
     * Print out the given time with the given format
     * @param format, eg "EEE dd MMM yyyy HH:mm:ss"
     * @param when, the moment of time to display
     * @return formatted version of the given date.
     */
    public static String format(String format, DateTime when) {
        return forPattern(format).print(when);
    }
}
